
# $Id: User.pm 96 2013-03-25 07:47:45Z chengxin.cai $

use strict;

package Provision::User;

use Provision::Object;
use base 'Provision::Object';

use config ':all';
use instance ':all';

use Provision::Utils qw( stopInstance removeAllInstances );

my $script_loc = $nvpath . '/instance';

my @provisionSteps = (
    {
        title => 'Create user',
        cmd => $script_loc . '/managesquser',
        args => '--add',
    },
    {
        title => 'Generate sqconfig',
        cmd => $script_loc . '/sqconfig_gen',
    },
    {
        title => 'Init STAF',
        cmd => $script_loc . '/stafinit',
        dev_only => 1,
    },
    {
        title => 'Init Manually',
        cmd => $script_loc . '/manualinit',
        prod_only => 1,
    },
);

my @undoSteps = (
    {
        title => 'Stop instance',
        code => \&stopInstance, 
    },
    {
        title => 'Remove all instances',
        code => \&removeAllInstances,
    },
    {
        title => 'Delete user',
        cmd => $script_loc . '/managesquser',
        args => '--del', 
    },
    {
        title => 'Remove user from status file',
        cmd => $script_loc . '/rmuserstatus',
    },
);

sub new {
    my ($proto, $id) = @_;
    my $class = ref $proto || $proto;

    return $class->SUPER::new($id);

}

sub get_idarg { my $self = shift; return '--squser=' . $self->id(); }

sub create {
    my ($self, $test) = @_;

    my $usertype = get_instance_usertype( $self->id() );

    for my $step (@provisionSteps) {

        if ( $usertype eq 'development') {
            next if ($step->{prod_only});
        }
        else { # production
            next if ($step->{dev_only});
        }

        my $ret = performStep($self, $step, $test);

        return $ret if ( $ret != 0 );
    }

    return config::OK;
}

sub delete {
    my ($self, $test) = @_;

    return $self->SUPER::delete( \@undoSteps, $test );

}

1;

